import {
  getSystemUserList,
  getSystemUserCount,
  systemUserResetPassword,
  systemUserDelete,
} from '@/api/system/userConfig.js'
import { getSystemRoleList } from '@/api/system/roleConfig.js'

export default {
  methods: {
    async getSystemUserCount(params) {
      const res = await getSystemUserCount(params)
      // console.log('getSystemUserCount', res)
      if (res.data.status !== 0) {
        this.pagination.total = 0
        return
      }
      this.pagination.total = res.data.data

      if (this.pagination.total === 0) {
        this.formData.pageNo = 1
        /* pageNo 过大, 请求数据总量在当前页展示不出来时 进行重置 */
      } else if (this.pagination.total < (this.formData.pageNo - 1) * this.formData.pageSize) {
        this.formData.pageNo = Math.ceil(this.pagination.total / this.formData.pageSize)
      }
      this.getSystemUserList({ ...this.formData })
    },

    async getSystemUserList(params) {
      this.tableLoading = true
      this.selection = []
      params.orderBy = 'user_id'
      const res = await getSystemUserList(params)
      // console.log('getSystemUserList', res)
      if (res.data.status !== 0) {
        this.tableLoading = false
        return
      }

      this.tableData = Object.freeze(res.data.data || [])
      this.tableLoading = false
    },

    /* 角色列表 */
    async getSystemRoleList() {
      const res = await getSystemRoleList()
      if (res.data.status !== 0) return
      const data = res.data.data || []
      this.roles = Object.freeze(data)
    },

    /* 重置密码 */
    async systemUserResetPassword(params) {
      const res = await systemUserResetPassword(params)
      if (res.data.status === 0) {
        return { msg: 'success', data: res.data }
      } else {
        return { msg: 'failed', data: res.data }
      }
    },

    /* 删除用户 */
    async systemUserDelete(params) {
      const res = await systemUserDelete(params)
      if (res.data.status === 0) {
        return { msg: 'success', data: res.data }
      } else {
        return { msg: 'failed', data: res.data }
      }
    },
  },
}
